package com.xunyun.kotlinwc.widget

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.animation.AnimationUtils
import android.widget.ImageView
import android.widget.TextView
import com.xunyun.kotlinwc.R

/**
 * @author gl
 * @date 2021/9/9
 * @desc
 */
class LoadingDialog : Dialog {
    private var progressView: ImageView? = null
    private var contentText: TextView? = null
    private var mContext: Context? = null
    private val loadAnimation = AnimationUtils.loadAnimation(context, R.anim.loading)
    private var isFirst = true

    constructor(context: Context) : super(context) {
        this.mContext = context
        setContentView(R.layout.dialog_layout_loading)
        progressView = findViewById(R.id.progressView)
        contentText = findViewById(R.id.contentText)
        setCancelable(false)
        setCanceledOnTouchOutside(false)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    fun setContent(content: String?) {
        contentText!!.text = content
    }

    override fun show() {
        progressView!!.startAnimation(loadAnimation)
        super.show()
    }


    fun showReset() {
        if (isFirst) {
            show()
            isFirst = false
        }
    }

    /**
     * 重置显示
     */
    fun reset() {
        isFirst = true
    }

    /**
     * @param flag 是否外部点击可消失，true:可以，false：不消失
     */
    fun setOutCancelable(flag: Boolean) {
        setCancelable(flag)
        setCanceledOnTouchOutside(flag)
        show()
    }

    override fun dismiss() {
        if (isShowing) {
            progressView!!.clearAnimation()
            super.dismiss()
        }
    }
}